package aufgabe_01;

import java.io.BufferedOutputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.LineNumberReader;
import java.io.PrintStream;
import java.io.ObjectInputStream.GetField;
import java.util.StringTokenizer;

public class DatenFilter {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		if (args.length < 1) {
			System.out.println("Bitte mind. eine Datei eingeben");
			System.exit(1);
		}
		try {
			LineNumberReader reader = new LineNumberReader(new FileReader(
					args[0]));
			BufferedOutputStream out = new BufferedOutputStream(System.out, 100);
			PrintStream print = new PrintStream(out);
			FileWriter fw = null;// = new FileWriter("datentest.txt");
			FileWriterFactory fwfac = FileWriterFactory.getInstance();
			String str;
			String substr;
			// StringTokenizer tok = new StringTokenizer();
			while ((str = reader.readLine()) != null) {
				if (str.indexOf(" ") != -1) {
					substr = str.substring(0, str.indexOf(" "));
					fw = fwfac.getFileWriter(substr + ".dat");
					fw.write( str + "\r\n" );                                                                 
					
				}
			}

			//fw.close();
			fwfac.close();
		} catch (FileNotFoundException fnfe) {
			System.out.println("Datei nicht gefunden");
		} catch (Exception e) {
			System.out.println(e.toString());
		} catch (Throwable e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
